package ai.moran.demo.thrift;

import java.util.concurrent.CountDownLatch;

import java.util.concurrent.TimeUnit;


import org.apache.thrift.TApplicationException;
import org.apache.thrift.TException;

import org.apache.thrift.async.AsyncMethodCallback;

import org.apache.thrift.async.TAsyncClientManager;

import org.apache.thrift.protocol.TBinaryProtocol;

import org.apache.thrift.protocol.TCompactProtocol;

import org.apache.thrift.protocol.TProtocol;

import org.apache.thrift.protocol.TProtocolFactory;

import org.apache.thrift.transport.TNonblockingSocket;

import org.apache.thrift.transport.TNonblockingTransport;

import org.apache.thrift.transport.TSocket;

import org.apache.thrift.transport.TTransport;

import org.apache.thrift.transport.TTransportException;


 

public class UserInfoClientDemo {

    public static final String SERVER_IP = "localhost";

    public static final int SERVER_PORT = 8081;

    public static final int SERVER_PORT1 = 8091;

    public static final int SERVER_PORT2 = 8092;

    public static final int SERVER_PORT3 = 8093;

    public static final int TIMEOUT = 30000;

 

    public void startClient(int userid) {

       TTransport transport = null;

       try {

           transport = new TSocket(SERVER_IP, SERVER_PORT, TIMEOUT);

           // 协议要和服务端一致

           TProtocol protocol = new TBinaryProtocol(transport);

           // TProtocol protocol = new TCompactProtocol(transport);

           // TProtocol protocol = new TJSONProtocol(transport);

           UserInfoService.Client client = new UserInfoService.Client(protocol);

           transport.open();

           String result = client.getUserNameById(userid);

           System.out.println("Thrify client result =: " + result);

       } catch (TTransportException e) {

           e.printStackTrace();

       } catch (TException e) {

          

                  //处理服务端返回值为null问题

                  if (e instanceof TApplicationException

                        && ((TApplicationException) e).getType() ==  

                                 TApplicationException.MISSING_RESULT) {

                System.out.println("The result of lg_userinfo_getUserNameById function is NULL");

            }

       } finally {

           if (null != transport) {

              transport.close();

           }

       }

    }

   


    public class AsynCallback implements AsyncMethodCallback<UserInfoService.AsyncClient.getUserNameById_call>{

       private CountDownLatch latch;

 

       public AsynCallback(CountDownLatch latch) {

           this.latch = latch;

       }

 

       @Override

       public void onComplete(UserInfoService.AsyncClient.getUserNameById_call response) {

           System.out.println("onComplete");

           try {

              Thread.sleep(1000L * 1);

              System.out.println("AsynCall result =:" + response.getResult().toString());

           } catch (TException e) {

              e.printStackTrace();

           } catch (Exception e) {

              e.printStackTrace();

           } finally {

              latch.countDown();

           }

       }

 

       @Override

       public void onError(Exception exception) {

           System.out.println("onError :" + exception.getMessage());

           latch.countDown();

       }

    }

 

    public static void main(String[] args) {

       UserInfoClientDemo client = new UserInfoClientDemo();

       client.startClient(1);

       client.startClient(2);

       client.startClient(3);

    }

}